﻿using System;
using System.IO;
using System.Collections.Generic;
using Nevron.Diagram;
using Nevron.Diagram.Extensions;
using Nevron.Diagram.WinForm.Commands;
using Nevron.GraphicsCore;
namespace ActiveFramework.CADGraphWinFormExt
{
    public partial class MetaGraphForm : DevExpress.XtraEditors.XtraForm
    {
        public MemoryStream DocumentStream { get; set; }
        public bool ActivatedDocument { get; set; }
        public string MetaControlCd { get; set; }
        public Guid Id { get; set; }
        
        private SaveToDBCommand svtdb;

        public MetaGraphForm()
        {
            InitializeComponent();
        }

        private void MetaGraphForm_Load(object sender, EventArgs e)
        {
            NDiagramCommandBarsManager manager = this.NBarsManager1;
            NDiagramCommander commander = manager.Commander;

            // create the predefined Basic shapes with size 20, 20 in millimeters
            nLibraryBrowser1.NewBasicShapesLibraryGroup(NGraphicsUnit.Millimeter, new NSizeF(20, 20));

            // create the predefined flowcharting shapes with size 20, 20 in millimeters
            nLibraryBrowser1.NewFlowchartingShapesLibraryGroup(NGraphicsUnit.Millimeter, new NSizeF(20, 20));

            // create the predefined connector shapes with size 20, 20 in millimeters
            nLibraryBrowser1.NewConnectorShapesLibraryGroup(NGraphicsUnit.Millimeter, new NSizeF(20, 20));


            // Entreprise edition
            //// create the predefined connector shapes with size 20, 20 in millimeters
            //nLibraryBrowser1.NewBrainstormingShapesLibraryGroup (NGraphicsUnit.Millimeter, new NSizeF(20, 20));

            //// create the predefined Food shapes with size 20, 20 in millimeters
            //nLibraryBrowser1.NewFoodShapesLibraryGroup(NGraphicsUnit.Millimeter, new NSizeF(20, 20));

            //// create the predefined Food shapes with size 20, 20 in millimeters
            //nLibraryBrowser1.NewFloorPlanShapesLibraryGroup(NGraphicsUnit.Millimeter );

            svtdb = new SaveToDBCommand(MetaControlCd, Id, ActivatedDocument);

            if (DocumentStream != null)
            {
                NPersistencyManager persistencyManager = new NPersistencyManager();
                NPersistentDocument pdoc = new NPersistentDocument();
                if (persistencyManager.LoadFromStream(DocumentStream, Nevron.Serialization.PersistencyFormat.Binary, null))
                {
                    pdoc = persistencyManager.PersistentDocument;
                    this.NDocument1 = (NDrawingDocument)pdoc.Sections[0].Object;
                    this.NDrawingView1.Document = this.NDocument1;
                    this.NDrawingView1.Refresh();
                }
            }

            if (ActivatedDocument == true)
            {
                this.NDrawingView1.Enabled = false;
            }

            NSaveDrawingCommand newSaveDrawingCmd = (NSaveDrawingCommand)commander.Commands.GetCommandFromId((int)DiagramCommand.SaveDrawing);
            int index = commander.Commands.IndexOf(newSaveDrawingCmd);
            commander.Commands.RemoveAt(index);
            commander.Commands.Insert(index, svtdb);
        }

        private void hideContainerLeft_Click(object sender, EventArgs e)
        {

        }
    }
}

